package com.realjinge.service;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.realjinge.api.MpApi;
import com.realjinge.dao.WeChatUserDao;
import com.realjinge.entity.WeChatUser;
import com.realjinge.message.MessageUtils;
import com.realjinge.message.MsgType;
import com.realjinge.message.MsgXmlResp;

/**
 * 回调请求处理
 * 
 * @author realjinge
 * 
 */
@Service
public class CallbackService {
	@Resource
	private WeChatUserDao weChatUserDao;

	private String PRODUCT_MANUAL_KEY = "PRODUCT_MANUAL_KEY";
	private String VERSION_CUSTOMER_KEY = "VERSION_CUSTOMER_KEY";

	/**
	 * 
	 * @param reqMap
	 * @return
	 */
	@SuppressWarnings("unused")
	public String handle(Map<String, String> reqMap) {
		String msgType = reqMap.get("MsgType");
		String fromUserName = reqMap.get("FromUserName");
		String toUserName = reqMap.get("ToUserName");

		// 文本信息
		if (MsgType.TEXT.equals(msgType)) {
			String content = reqMap.get("Content");
			return MsgXmlResp.buildText(fromUserName, toUserName, content);
		}

		// 语音信息
		if (MsgType.VOICE.equals(msgType)) {
			return MsgXmlResp.buildText(fromUserName, toUserName, "不晓得嗯在港么东西");
		}

		// 基础事件推送
		if (MsgType.EVENT.equals(msgType)) {
			String event = reqMap.get("Event");
			// 关注公众号
			if (MsgType.Event.SUBSCRIBE.equals(event)) {
				WeChatUser user = weChatUserDao.selectWeChatUserByOpenID(fromUserName);
				if (user == null) {
					WeChatUser weChatUser = MpApi.getUserInfo(fromUserName);
					if (weChatUser != null) {
						weChatUserDao.insertWeChatUser(weChatUser);
					}
				}
				// 回复欢迎语
				return MsgXmlResp.buildText(fromUserName, toUserName, "欢迎关注real进歌微信测试公众号！");
			}

			// 菜单点击事件
			if (MsgType.Event.CLICK.equals(event)) {
				// 根据key值判断点击的哪个菜单
				String eventKey = reqMap.get("EventKey");
				// 产品手册
				if (PRODUCT_MANUAL_KEY.equals(eventKey)) {
					return MessageUtils.initProductInfoNews(fromUserName, toUserName);
				}
				// 版本与客服
				if (VERSION_CUSTOMER_KEY.equals(eventKey)) {
					return MsgXmlResp.buildText(fromUserName, toUserName,
							"版本信息：\nv1.0.0.20170410_demo\n联系我们：\nhttp://www.realjinge.com/");
				}
				return "";
			}
		}

		// 设备消息或事件
		if (MsgType.DEVICE_EVENT.equals(msgType) || MsgType.DEVICE_TEXT.equals(msgType)) {
			String reqContent = reqMap.get("Content");
			String deviceType = reqMap.get("DeviceType");
			String deviceID = reqMap.get("DeviceID");
			String sessionID = reqMap.get("SessionID");
			final String openID = reqMap.get("OpenID");
			// 设备事件推送
			if (MsgType.DEVICE_EVENT.equals(msgType)) {
				String event = reqMap.get("Event");
				// 绑定/解绑事件
				if (MsgType.DeviceEvent.BIND.equals(event) || MsgType.DeviceEvent.UNBIND.equals(event)) {
					// 存储用户和设备的绑定关系
					if (MsgType.DeviceEvent.BIND.equals(event)) {

					} else {

					}
					// 设备绑定/解绑事件可以回复空包体
					return "";
				}
				// 收到设备消息
				if (MsgType.DEVICE_TEXT.equals(msgType)) {
					System.out.println("收到设备消息" + reqContent);
				}

			}

		}

		// 未处理的情况返回空字符串
		return "";
	}

}
